package com.mmlc.meimei.lichengkongjian.brand;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.Adapter.ConditionAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.Condition;
import com.mmlc.meimei.lichengkongjian.customview.CategoryListView;
import com.mmlc.network.filter.ConditionRequestFilter;
import com.mmlc.network.responsebean.ConditionResponseBean;
import com.mmlc.utils.StringUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.widget.BrandView;
import com.mmlc.widget.MyGridView;

/*
 *@author 易旭
 *desc:筛选条件界面
 *@modification
 *Creat at 2016/9/30  10:20
 *modification at
 */
public class ConditionFragment extends InterFragment {
    private EditText etminprice;
    private EditText etmaxprice;
    private EditText etkeyword;
    private TextView tvclassify;
    private LinearLayout llclassify;
    private ImageView iv_next;
    private TextView tvbrand;
    private LinearLayout llbrand;
    private MyGridView gridseason;
    private MyGridView gridyear;
    private TextView tvreset;
    private TextView tvconfirm;

    private TextView tvseason;
    private TextView tvyear;

    private BrandView bv;

    private CategoryListView clv;

    private Condition condition;

    private String categoeryId = "";
    private String brandId = "";
    private String categoeryName = "";
    private String brandName = "";
    private String keyword = "";
    private int filterType;
    private ConditionAdapter<String> yearsAdapter;

    private ConditionAdapter<String> seasonAdapter;
    private ImageView brandrightarrow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        condition = new Condition();
        if (getArguments() != null) {
            categoeryId = getArguments().getString("categoeryId");
            brandId = getArguments().getString("brandId");
            categoeryName = getArguments().getString("categoeryName");
            brandName = getArguments().getString("brandName");
            keyword = getArguments().getString("keyword");
            filterType = getArguments().getInt("filterType");
            condition.setCategoeryId(categoeryId);
            condition.setBrandId(brandId);
            condition.setCategoeryName(categoeryName);
            condition.setBrandName(brandName);
        }
        yearsAdapter = new ConditionAdapter<String>(getActivity());
        seasonAdapter = new ConditionAdapter<String>(getActivity());
    }

    @Override
    protected int initResourceID() {
        return R.layout.fragment_condition;
    }

    @Override
    protected void initView() {
        bv = (BrandView) view.findViewById(R.id.bv_brand);
        bv.setData(brandId, brandName);
        brandrightarrow = (ImageView) view.findViewById(R.id.brand_right_arrow);
        clv = (CategoryListView) view.findViewById(R.id.clv_categoery);
        etminprice = (EditText) view.findViewById(R.id.et_min_price);
        etkeyword = (EditText) view.findViewById(R.id.et_keyword);
        etmaxprice = (EditText) view.findViewById(R.id.et_max_price);
        tvclassify = (TextView) view.findViewById(R.id.tv_classify);
        tvseason = (TextView) view.findViewById(R.id.tv_season);
        tvyear = (TextView) view.findViewById(R.id.tv_year);
        llclassify = (LinearLayout) view.findViewById(R.id.ll_classify);
        iv_next = (ImageView) view.findViewById(R.id.iv_next);

        tvbrand = (TextView) view.findViewById(R.id.tv_brand);
        llbrand = (LinearLayout) view.findViewById(R.id.ll_brand);
        llbrand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bv.animInView();
            }
        });
        gridseason = (MyGridView) view.findViewById(R.id.grid_season);
        gridseason.setAdapter(seasonAdapter);
        gridyear = (MyGridView) view.findViewById(R.id.grid_year);
        gridyear.setAdapter(yearsAdapter);
        tvreset = (TextView) view.findViewById(R.id.tv_reset);
        tvreset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetCondition();
            }
        });
        tvconfirm = (TextView) view.findViewById(R.id.tv_confirm);
        tvconfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmConditon();
            }
        });
        setBCData();
        getConditionData();
        bv.setChildClickedListener(new BrandView.ChildClickedListener() {
            @Override
            public void childClicked(String brandId, String brandName) {
                setBrand(brandId, brandName);
            }
        });
        clv.setSelectClickedListener(new CategoryListView.SelectClickedListener() {
            @Override
            public void onClicked(String pid, String text) {
                setCategoery(pid, text);
            }


        });
        if (!TextUtils.isEmpty(condition.getBrandName())) {
            brandrightarrow.setVisibility(View.INVISIBLE);
            llbrand.setClickable(false);
        } else {
            brandrightarrow.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(keyword))
        {
            etkeyword.setText(keyword);
        }
        clv.setInitData(categoeryId, categoeryName,brandId,keyword);
        if (TextUtils.isEmpty(categoeryId))
            clv.setAllSelectable(true);
        processData();
    }

    BrandInfoActivity activity;
    private void processData() {
        Activity activity = getActivity();
        if(activity instanceof BrandInfoActivity){
            this.activity = (BrandInfoActivity) activity;
        }
        //处理品牌
        processBrand();
        
        //处理分类
        processCategory();

    }

    private void processBrand() {

        //如果是从首页过来的，那么就不需要
//        if(activity == null || TextUtils.isEmpty(activity.cate_id)){
//            return;
//        }

//        bv.getData(activity.cate_id);
        bv.getData(keyword);
    }

    private void processCategory() {
//        if(filterType == 0){//表示分类中的筛选
//            iv_next.setVisibility(View.GONE);
//        }else{
//        }
        llclassify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keyword=etkeyword.getText().toString();
                clv.animInView(categoeryId,categoeryName,brandId,keyword);
            }
        });
    }

    /*
     *@author 易旭
     *desc: 确认时生成新的years，season字符串，以逗号隔开
     *@modification
     *Creat at 2016/9/30  10:21
     *modification at
     *params
     *return
     */
    private void confirmConditon() {

        if (!TextUtils.isEmpty(etminprice.getText().toString())
                && Long.valueOf(etminprice.getText().toString()) < 0 && !TextUtils.isEmpty(etmaxprice.getText().toString())
                && Long.valueOf(etmaxprice.getText().toString()) < 0) {
            ToastUtils.showToast("不能输入低于零的数");
            return;
        }
        keyword=etkeyword.getText().toString();
        condition.setKeyword(etkeyword.getText().toString());
        condition.setMin(etminprice.getText().toString());
        condition.setMax(etmaxprice.getText().toString());
        condition.setSeasonId(StringUtils.arrayToString(seasonAdapter.getSelectList()));
        condition.setYearId(StringUtils.arrayToString(yearsAdapter.getSelectList()));
        if (mOnDataChangeListener != null) {
            mOnDataChangeListener.onChange(condition);
            mOnDataChangeListener.close();
        }
    }

    /*
     *@author 易旭
     *desc:选择品牌后，更新界面以及数据
     *@modification
     *Creat at 2016/9/30  10:22
     *modification at
     *params
     *return
     */
    private void setBrand(String brandId, String brandName) {
        if (TextUtils.equals(brandId, condition.getBrandId()))
            return;
        condition.setBrandId(brandId);
        condition.setBrandName(brandName);
        setBCData();
        getConditionData();
    }

    private void setCategoery(String categoeryId, String categoeryName) {
        if (TextUtils.equals(categoeryId, condition.getCategoeryId()))
            return;
        condition.setCategoeryId(categoeryId);
        condition.setCategoeryName(categoeryName);
        setBCData();
        getConditionData();
    }

    private void setBCData() {
        if (!TextUtils.isEmpty(condition.getCategoeryName())) {
            tvclassify.setText(condition.getCategoeryName());
            tvclassify.setTextColor(getResources().getColor(R.color.text_red));
        } else {
            tvclassify.setText("全部");
            tvclassify.setTextColor(getResources().getColor(R.color.text_grey));
        }
        if (!TextUtils.isEmpty(condition.getBrandName())) {
            tvbrand.setText(condition.getBrandName());
            tvbrand.setTextColor(getResources().getColor(R.color.text_red));
        } else {
            tvbrand.setText("全部");
            tvbrand.setTextColor(getResources().getColor(R.color.text_grey));
        }
    }


    private void resetCondition() {
        condition.setMin("");
        condition.setMax("");
        condition.setBrandId(brandId);
        condition.setCategoeryId(categoeryId);
        condition.setBrandName(brandName);
        condition.setCategoeryName(categoeryName);
        condition.setKeyword("");
        condition.setYearId("");
        condition.setSeasonId("");
        etminprice.setText("");
        etmaxprice.setText("");
        etkeyword.setText("");
        seasonAdapter.getSelectList().clear();
        yearsAdapter.getSelectList().clear();
        yearsAdapter.notifyDataSetChanged();
        seasonAdapter.notifyDataSetChanged();

        if (mOnDataChangeListener != null) {
            mOnDataChangeListener.onChange(condition);
            mOnDataChangeListener.close();
        }
        setBCData();
        getConditionData();
    }


    private void getConditionData() {
        yearsAdapter.getSelectList().clear();
        seasonAdapter.getSelectList().clear();
        ConditionRequestFilter filter = new ConditionRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.brand_id = condition.getBrandId();
        filter.requestBean.cate_id = condition.getCategoeryId();
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ConditionResponseBean>() {
            @Override
            public void onSuccess(ConditionResponseBean result) {
                yearsAdapter.getList().clear();
                seasonAdapter.getList().clear();
                if (TextUtils.equals(String.valueOf(result.getCode()), "0")) {
                    yearsAdapter.setList(result.getData().getYears());
                    seasonAdapter.setList(result.getData().getSeason());
                }
                if (result.getData().getYears().size() == 0)
                    tvyear.setVisibility(View.GONE);
                else
                    tvyear.setVisibility(View.VISIBLE);
                if (result.getData().getSeason().size() == 0)
                    tvseason.setVisibility(View.GONE);
                else
                    tvseason.setVisibility(View.VISIBLE);
                yearsAdapter.notifyDataSetChanged();
                seasonAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                yearsAdapter.getList().clear();
                yearsAdapter.notifyDataSetChanged();
                seasonAdapter.getList().clear();
                seasonAdapter.notifyDataSetChanged();
            }
        });
    }


    private OnDataChangeListener mOnDataChangeListener;

    public OnDataChangeListener getOnDataChangeListener() {
        return mOnDataChangeListener;
    }

    public void setOnDataChangeListener(OnDataChangeListener onDataChangeListener) {
        mOnDataChangeListener = onDataChangeListener;
    }


    public interface OnDataChangeListener {
        public void onChange(Condition condition);

        public void close();
    }


}
